package com.base.controller;

import com.base.comm.CommContent;
import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 抽奖
 *
 * @author 111
 */
@Controller
@RequestMapping("/admin")
public class Index_DZP {

    // 商品信息
    @Autowired
    private ApiinfoesService apiinfoesService;
    @Autowired
    private SubscribersService subscribersService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private DialsService dialsService;
    @Autowired
    private CallbackinfoesService callbackinfoesService;
    @Autowired
    private ChangeRecordService changeRecordService;
    @Autowired
    private LogsService logsService;// log日志

    @RequestMapping("/dzpIndex")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        Page page = new Page();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        List<Dials> dials = null;

        switch (adminUsergrade) {
            case 0:
                page.setCurrentPageNo(1);
//                page.setRecordTotal(dialsService.countAll(null, null, null, (byte)1));
                page.setRecordTotal(50000);
                dials = dialsService.selectAll(page, null, null, null, (byte)1);
                break;
            case 1:
                page.setCurrentPageNo(1);
//                page.setRecordTotal(dialsService.countAll(adminUserId, null, null, (byte)1));
                page.setRecordTotal(50000);
                dials = dialsService.selectAll(page, adminUserId, null, null, (byte)1);
                break;
        }

        /*
         * 设置分页
         */
        int totlenum = page.getRecordTotal();// 页面总数
        request.setAttribute("totlenum", totlenum);// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中

        if (dials != null) {
            for (Dials d : dials){
                Users users = usersService.selectByPrimaryKey(d.getParentid());
                d.setParentid(users.getUsername());
            }
        }

        // 设置页面进行跳转
        request.setAttribute("dials", dials);
        modelAndView.setViewName("jsp/index_DZP");
        return modelAndView;
    }

    @RequestMapping("/selectDZP")
    public ModelAndView selectDZP(HttpServletRequest request, String username, String mobile, Integer curPage) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        Page page = new Page();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        List<Dials> dials = null;

        String fUserName = null;
        String fMobile = null;
        if (username != null && !username.equals("") && !username.equals("underfined")){
            fUserName = username;
        }
        if (mobile != null && !mobile.equals("") && !mobile.equals("underfined")){
            fMobile = username;
        }

        switch (adminUsergrade) {
            case 0:
                page.setCurrentPageNo(curPage);
//                page.setRecordTotal(dialsService.countAll(null, fUserName, fMobile, (byte)1));
                page.setRecordTotal(50000);
                dials = dialsService.selectAll(page, null, fUserName, fMobile, (byte)1);
                break;
            case 1:
                page.setCurrentPageNo(curPage);
//                page.setRecordTotal(dialsService.countAll(adminUserId, fUserName, fMobile, (byte)1));
                page.setRecordTotal(50000);
                dials = dialsService.selectAll(page, adminUserId, fUserName, fMobile, (byte)1);
                break;
        }
        /*
         * 设置分页
         */
        int totlenum = page.getRecordTotal();// 页面总数
        request.setAttribute("totlenum", totlenum);// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中

        if (dials != null) {
            for (Dials d : dials){
                Users users = usersService.selectByPrimaryKey(d.getParentid());
                d.setParentid(users.getUsername());
            }
        }

        request.setAttribute("mobile", mobile);
        request.setAttribute("username", username);

        // 设置页面进行跳转
        request.setAttribute("dials", dials);
        modelAndView.setViewName("jsp/index_DZP");
        return modelAndView;
    }
}
